/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.catalog.app.compose.ui

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material.icons.rounded.Check
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.Card
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.gitee.wsl.compose.ui.base.Drawer
import com.gitee.wsl.compose.ui.drawer.Card
import com.gitee.wsl.compose.ui.drawer.CardDrawerState
import com.gitee.wsl.compose.ui.drawer.rememberCardDrawerState
import com.google.android.catalog.framework.annotations.Sample
import kotlinx.coroutines.launch

@Sample(
    name = "Compose card drawer sample",
    description = "Shows how to add a compose target in the catalog",
    documentation = "https://github.com/google/casa-android#create-sample-modules"
)
@Composable
fun CardDrawerSample() {
        val drawerState = rememberCardDrawerState(initialValue = DrawerValue.Closed)
        Drawer.Card(
            contentCornerSize = 16.dp,
            drawerContent = { DrawerContent(drawerState) },
            drawerBackgroundColor = DrawerBackground,
            drawerState = drawerState
        ) {
            MainScreen(drawerState)
        }
}

val DrawerBackground = Color.Gray

@Composable
private fun MainScreen(drawerState: CardDrawerState) {
    val coroutineScope = rememberCoroutineScope()

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(top = 16.dp, start = 16.dp, end = 16.dp),
    ) {

        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween
        ) {

            Card(
                onClick = { coroutineScope.launch { drawerState.open() } },
                modifier = Modifier.size(50.dp),
                shape = RoundedCornerShape(12.dp)
            ) {
                Icon(
                   Icons.Rounded.Add,
                    null,
                    tint = DrawerBackground,
                    modifier = Modifier
                        .padding(12.dp)
                )
            }


            Card(
                modifier = Modifier.size(50.dp),
                shape = RoundedCornerShape(12.dp),
            ) {
                Icon(
                    Icons.Rounded.Search,
                    null,
                    tint = DrawerBackground,
                    modifier = Modifier
                        .padding(12.dp)
                )
            }

        }

        Spacer(modifier = Modifier.padding(8.dp))

        Text(
            text = "Best Suits",
            style = MaterialTheme.typography.headlineMedium,
        )

        Text(
            text = "Perfect Choice!",
            style = MaterialTheme.typography.headlineMedium,
        )
        Spacer(modifier = Modifier.padding(8.dp))


        Row() {


            Card(
                modifier = Modifier
                    .fillMaxWidth(.8f)
                    .height(50.dp),
                shape = RoundedCornerShape(12.dp),
            ) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(4.dp),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Start
                ) {

                    Icon(
                        Icons.Rounded.Search,
                        null,
                        modifier = Modifier.padding(8.dp)
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    Text(
                        text = "Search", style = MaterialTheme.typography.bodyMedium,
                        //color = OnBackground2
                    )
                }
            }

            Spacer(modifier = Modifier.padding(8.dp))

        }

    }
}


@Composable
fun DrawerContent(drawerState: CardDrawerState) {
    val coroutineScope = rememberCoroutineScope()

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(8.dp)
    ) {

        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.Start
            ) {


                Spacer(modifier = Modifier.width(8.dp))

                Column() {
                    Text(
                        text = "Mahdi R.",
                        style = MaterialTheme.typography.bodyMedium,
                        color = Color.White
                    )
                    Text(
                        text = "Android Developer",
                        style = MaterialTheme.typography.bodyMedium,
                        color = Color.White
                    )
                }
            }

            Card(
                onClick = { coroutineScope.launch { drawerState.close() } },
                shape = CircleShape
            ) {
                Icon(
                    Icons.Rounded.Check,
                    null,
                    tint = DrawerBackground,
                    modifier = Modifier
                        .padding(4.dp)
                        .size(24.dp)
                )
            }
        }

        Spacer(modifier = Modifier.height(64.dp))

        Text(
            text = "Browse",
            style = MaterialTheme.typography.titleSmall,
            color = Color.White
        )

        Spacer(modifier = Modifier.height(16.dp))

        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Start
        ) {

            Spacer(modifier = Modifier.width(8.dp))
            Text(
                text = "Home", style = MaterialTheme.typography.bodyMedium,
                color = Color.White
            )
        }
    }

}
